<!-- --------分享-------- -->
<template>
	<view class="share">
		<image
			:style="{ width: `100%`, borderRadius: `10rpx` }"
			:src="url"
			mode="widthFix"
			@load="loadCompleted"
			@error="loadFail"
		/>
		<view class="btn" @click="saveImageToPhotosAlbum">保存到相册</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url: "",
			};
		},
		onLoad() {
			uni.showToast({ title: "Loading...", icon: "loading" });
			this.$request.get("/Home/info/getConfig/").then(({ data }) => {
				this.url = data.yq[0];
			});
		},
		methods: {
			loadCompleted() {
				uni.hideToast();
			},
			loadFail() {
				uni.showToast({ title: "图片加载失败", icon: "none" });
			},
			saveImageToPhotosAlbum() {
				uni.showToast({ title: "Download...", icon: "loading" });
				uni.downloadFile({
					url: this.url,
					success: ({ tempFilePath: filePath }) => {
						uni.saveImageToPhotosAlbum({
							filePath,
							success: () => uni.showToast({ title: "已保存至系统相册" }),
							fail: () =>
								uni.showToast({
									title: "图片保存失败",
									icon: "none",
								}),
						});
					},
					fail: () =>
						uni.showToast({
							title: "图片下载失败",
							icon: "none",
						}),
				});
			},
		},
	};
</script>

<style lang="scss">
	.share {
		padding: 30rpx;
	}
	.btn {
		height: 80rpx;
		background: #2a62ff;
		border-radius: 10rpx;
		text-align: center;
		line-height: 80rpx;
		color: #ffffff;
		font-size: 34rpx;
		font-family: Alibaba PuHuiTi;
		font-weight: 400;
		margin-top: 60rpx;
	}
</style>
